﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutChild.cshtml";
}
@section head {
    @if (false)
    {<script src="~/Scripts/jquery-1.6.4-vsdoc.js" type="text/javascript"></script>}
    <style type="text/css">
        td
        {
            padding: 10px;
            border: 1px solid #ece9d8;
        }
        .list
        {
            float: left;
            width: 22%;
            padding: 2px;
        }
        .tablecontent
        {
            border-collapse: collapse;
            border: 1px solid #ece9d8;
        }
        .hover
        {
            background: #e0eccc;
        }
        .tdcenter
        {
            text-align: center;
        }
    </style>
    <script language="javascript" type="text/javascript">
        var selectId = "";
        var gridId = "#tableId";
        var treeObj;
        var cycle = "@ViewBag.CYCLE";
        var reportid = "@ViewBag.ReportID";
        $(document).ready(function () {
            loadOrganizationNoCheckParent(true); //checkenable 节点前有复选框
            loadyear();
            loadmonth();
            loaddefautdate();
            loadzb();
            $("#selectAllzb").click(function () {
                if ($(this).attr("checked"))
                    $("#ZbContent :checkbox").attr("checked", "checked");
                else
                    $("#ZbContent :checkbox").attr("checked",false);
            });
            $("#selectAllwd").click(function () {
                if ($(this).attr("checked"))
                    $("#WdContent :checkbox").attr("checked", "checked");
                else
                    $("#WdContent :checkbox").attr("checked", false);
            });
        });

        function loaddefautdate() {
            var year, month, season, nb_year, jb_year, jb_season, yb_year, yb_month;
              var myDate = new Date();
            year=myDate.getFullYear();
            month = myDate.getMonth()+1;//月份默认从0开始
            season = Math.ceil(month / 3);

            if (cycle == "01") {
                if (month == 1) {
                    yb_year = year - 1;
                    yb_month = 12;
                }
                else {
                    yb_year = year;
                    yb_month = month - 1;
                    if (yb_month < 10) 
                    yb_month = '0' + yb_month;
                }
                $("#yearselect").val(yb_year); 
                $("#yearendselect").val(yb_year);
                $("#monthselect").val(yb_month);
                $("#monthendselect").val(yb_month);

            } else if (cycle == "02") {
                    if (season == 1) {
                        jb_year = year - 1;
                        jb_season = '16';
                }
                    else {
                        jb_year = year;
                        jb_season =12+ season - 1;
                    }
                    $("#yearselect").val(jb_year);
                    $("#yearendselect").val(jb_year);
                    $("#monthselect").val(jb_season);
                    $("#monthendselect").val(jb_season);

            }
                else {
                    nb_year = year - 1;
                    $("#yearselect").val(nb_year);
                    $("#yearendselect").val(nb_year);
                    
            }

            
        }

        function loadyear() {
            var yearselect = $("#yearselect");
            var yearendselect = $("#yearendselect");
            var myDate = new Date();
            var startyear = myDate.getFullYear() - 5;
            var endyear = myDate.getFullYear() + 2
            for (var i = startyear; i <= endyear; i++) {
                var option = $("<option>").text(i + "年").val(i);
                yearselect.append(option);
            }
            yearendselect.html(yearselect.html());
            $("#yearselect option[value='" + myDate.getFullYear() + "']").attr("selected", true)
            $("#yearendselect option[value='" + myDate.getFullYear() + "']").attr("selected", true)

        }
        function loadmonth() {
            var monthselect = $("#monthselect");
            var monthendselect = $("#monthendselect")
            if (cycle == "01") {
                $("#cycle_month").show();
                $("#whichTime").text("月份:")
                for (var i = 1; i < 13; i++) {
                    var option = $("<option>").text(i + "月").val(i < 10 ? ('0' + i) : i);
                    monthselect.append(option);
                }
            }
            else if (cycle == "02") {
                $("#cycle_month").show();
                $("#whichTime").text("季度:")
                for (var i = 13; i < 17; i++) {
                    var option = $("<option>").text("第" + (i - 12) + "季度").val(i)
                    monthselect.append(option);
                }
            }
            else {
                $("#cycle_month").hide();
            }

            monthendselect.html(monthselect.html());
        }

        function zTreeOnClick(event, treeId, treeNode, clickFlag) {
            curSelectNode = treeNode;
        }
        function zTreeMenuCheck(event, treeId, treeNode) {
            //           if (treeNode.checked && treeNode.getParentNode() != null) {//当勾选节点时，同是勾选父节点本身。
            //               //只勾选父节点本身，无任何勾选联动操，触发 beforeCheck & onCheck 事件回调函数，这样可以自动勾选父节点的父节点。
            //               treeObj.checkNode(treeNode.getParentNode(), true, false, true);
            //           }
        }

        function zTreeOnAsyncSuccess(event, treeId, treeNode) {
            treeObj = $.fn.zTree.getZTreeObj(treeId);
            $("#tree_load").hide();
        }

        function loadzb() {
            $.ajax({
                type: "POST",
                url: "/DataManage/ReportAnalyse/GetReportTarget?reportid=@ViewBag.ReportID",
                success: function (msg) {
                    var data = eval(msg);
                    $.each(data, function (index, d) {
                        var id = "zb" + index;
                        $("#ZbContent").append("<span class='list'><input type='checkbox' id='" + id + "' value='" + d.NAME + "' name='zblist'/><label for='" + id + "'>" + d.NAME + "</label></span>");
                    })

                    loadwd();
                }
            });
        }

        function loadwd() {
            $.ajax({
                type: "POST",
                url: "/DataManage/ReportAnalyse/GetReportLatitude?reportid=@ViewBag.ReportID",
                success: function (msg) {
                    var data = eval(msg);
                    $.each(data, function (index, d) {
                        var id = "wd" + index;
                        $("#WdContent").append("<span class='list'><input type='checkbox' id='" + id + "' value='" + d.NAME + "' name='wdlist'/><label for='" + id + "'>" + d.NAME + "</label></span>");
                    })
                    loadEvent();
                }
            });
        }

        function loadEvent() {
            $(".list").hover(function () {
                $(this).addClass("hover");
            }, function () {
                $(this).removeClass("hover");
            });
        }

        function ReportSearch() {
            if (treeObj == null)
                return;
            var nodes = treeObj.getCheckedNodes(true);

            if (nodes.length == 0) {
                alert("请选择要查询的单位!");
                return;
            }
            var OrgIds = "";
            for (var i = 0; i < nodes.length; i++) {
                OrgIds += nodes[i].ID + ",";
            }
            if (OrgIds.length > 0)
                OrgIds = OrgIds.substring(0, OrgIds.length - 1); //把最后一个逗号去掉
            var yearstr = $("#yearselect").val();
            var yearendstr = $("#yearendselect").val();

            if (yearstr > yearendstr) {
                alert("开始年度不能大于结束年度");
                return;
            }

            var monstr = $("#monthselect").val();
            var monendstr = $("#monthendselect").val();
            if (monstr > monendstr) {
                alert("开始日期不能大于结束日期");
                return;
            }
            var wdstr = "";
            var zbstr = "";
            $("input[name^='zblist']").each(function () {
                if (this.checked) {
                    zbstr = zbstr + $(this).val() + ",";
                }
            });
            $("input[name^='wdlist']").each(function () {
                if (this.checked) {
                    wdstr = wdstr + $(this).val() + ",";
                }
            });
            if (zbstr.length == 0) {
                alert("请选择指标!"); return;
            }
            if (wdstr.length == 0) {
                alert("请选择维度!"); return;
            }
            showparentloading();
            $.ajax({
                type: "POST",
                url: "/DataManage/ReportSearch/Search",
                data: "year=" + yearstr + "&yearend=" + yearendstr + "&month1=" + monstr + "&month2=" + monendstr + "&zb=" + encodeURI(zbstr) + "&wd=" + encodeURI(wdstr) + "&reportid=" + reportid + "&dwid=" + OrgIds,
                success: function (msg) {
                    hideparentloading();
                    parent.asyncbox.open({
                        title: "查询结果",
                        width: $(document).width() - 100,
                        height: $(document).height() - 50,
                        scroll: 'auto',
                        modal: true,
                        html: msg
                    });

                }
            });
        }

        function ReportExport() {
            if (treeObj == null)
                return;
            var nodes = treeObj.getCheckedNodes(true);
            if (nodes.length == 0) {
                alert("请选择要查询的单位!");
                return;
            }
            var OrgIds = "";
            for (var i = 0; i < nodes.length; i++) {
                OrgIds += nodes[i].ID + ",";
            }
            if (OrgIds.length > 0)
                OrgIds = OrgIds.substring(0, OrgIds.length - 1); //把最后一个逗号去掉
            var yearstr = $("#yearselect").val();
            var yearendstr = $("#yearendselect").val();

            if (yearstr > yearendstr) {
                alert("开始年度不能大于结束年度");
                return;
            }
            var monstr = $("#monthselect").val();
            var monendstr = $("#monthendselect").val();
            if (monstr > monendstr) {
                alert("开始日期不能大于结束日期");
                return;
            }
            var wdstr = "";
            var zbstr = "";
            $("input[name^='zblist']").each(function () {
                if (this.checked) {
                    zbstr = zbstr + $(this).val() + ",";
                }
            });
            $("input[name^='wdlist']").each(function () {
                if (this.checked) {
                    wdstr = wdstr + $(this).val() + ",";
                }
            });
            if (zbstr.length == 0) {
                alert("请选择指标!"); return;
            }
            if (wdstr.length == 0) {
                alert("请选择维度!"); return;
            }

            var zbcount = zbstr.substr(0,zbstr.length - 1).split(",");
            var wdcount = wdstr.substr(0,wdstr.length - 1).split(",");
            if (zbcount.length * wdcount.length > 255){
                    alert('所选指标和纬度之剩积不能超过255，否则Excel文件将无法容纳！');
                    return ;
                }
            //因为参数太长，不能直接用get方法传递，只能通过form传递
            var url="/DataManage/ReportSearch/Export";
            var data = "year=" + yearstr + "&yearend=" + yearendstr + "&month1=" + monstr + "&month2=" + monendstr + "&zb=" + encodeURI(zbstr) + "&wd=" + encodeURI(wdstr) + "&reportid=" + reportid + "&dwid=" + OrgIds;
            zbvalue = encodeURI(zbstr);
            wdvalue = encodeURI(wdstr);

            var form = $("<form>"); //定义一个form表单
            form.attr("style", "display:none");
            form.attr("target", "");
            form.attr("method", "post");
            form.attr("action", url);
            $("#frmDownFile").contents().find("body").append(form);
            //创建保存参数的文本域
            var year = $("<input>");
            year.attr("type", "hidden");
            year.attr("name", "year");
            year.attr("id", "year");
            year.attr("value", yearstr);
            var yearend = $("<input>");
            yearend.attr("type", "hidden");
            yearend.attr("name", "yearend");
            yearend.attr("id", "yearend");
            yearend.attr("value", yearendstr);
            var month1 = $("<input>");
            month1.attr("type", "hidden");
            month1.attr("name", "month1");
            month1.attr("id", "month1");
            month1.attr("value", monstr);
            var month2 = $("<input>");
            month2.attr("type", "hidden");
            month2.attr("name", "month2");
            month2.attr("id", "month2");
            month2.attr("value", monendstr);
            var zb = $("<input>");
            zb.attr("type", "hidden");
            zb.attr("name", "zb");
            zb.attr("id", "zb");
            zb.attr("value", zbvalue);
            var wd = $("<input>");
            wd.attr("type", "hidden");
            wd.attr("name", "wd");
            wd.attr("id", "wd");
            wd.attr("value", wdvalue);
            var _reportid = $("<input>");
            _reportid.attr("type", "hidden");
            _reportid.attr("name", "reportid");
            _reportid.attr("id", "reportid");
            _reportid.attr("value", reportid);
            var dwid = $("<input>");
            dwid.attr("type", "hidden");
            dwid.attr("name", "dwid");
            dwid.attr("id", "dwid");
            dwid.attr("value", OrgIds);
   
            form.append(year);
            form.append(yearend);
            form.append(month1);
            form.append(month2);
            form.append(zb);
            form.append(wd);
            form.append(_reportid);
            form.append(dwid);

            form.submit(); //表单提交 


            //            $.post("/DataManage/ReportSearch/Export", { year: yearstr, yearend: yearendstr, month1: monstr, month2: monendstr, zb: zb, wd: wd, reportid: reportid, dwid: OrgIds}, function (data, status) { 
            //            debugger;
            //            alert(date);
            //            });
            //            $("#frmDownFile").attr("src", url);
            //            showparentloading();
            //            $.ajax({
            //                type: "POST",
            //                url: "/DataManage/ReportSearch/Export",
            //                data: "year=" + yearstr + "&yearend=" + yearendstr + "&month1=" + monstr + "&month2=" + monendstr + "&zb=" + encodeURI(zbstr) + "&wd=" + encodeURI(wdstr) + "&reportid=" + reportid + "&dwid=" + OrgIds,
            //                success: function (msg) {
            //                    hideparentloading();
            //                }
            //            });


        }
    </script>
}
<!--头部导航,不用时可以去掉-->
<div class="ui-layout-north">
    <div id="headertitle">
    </div>
    <div class="buttonlist" style="float: right; height: 34px; line-height: 36px; margin-right: 20px;">
        <a onclick="ReportSearch()" class="button green"><span class="icon-botton" style="background: url('/Content/16/search.png') no-repeat scroll 0px 4px;">
        </span>查询</a> <a onclick="ReportExport()" class="button green"><span class="icon-botton"
            style="background: url('/Content/16/search.png') no-repeat scroll 0px 4px;"></span>
            导出</a>
    </div>
</div>
<div class="ui-layout-north">
</div>
<div class="ui-layout-west">
    <div id="tree_load">
        正在加载,请稍后...</div>
    @Html.Partial("_OrganizationCheckTree")
</div>
<!--主窗体，页面都需要的div-->
<div id="divGridContainer" class="ui-layout-center" style="overflow: auto;background: url(/Content/images/bj.gif); ">
    <div style="background: #eaeaea; padding: 8px; color: #ff0000">
        请选择查询条件:</div>
    <table style="width: 100%" class="tablecontent">
        <tr>
            <td style="width: 30px; font-weight: bold; text-align: center;">
                年度:
            </td>
            <td>
                <select style='width: 80px;' id="yearselect">
                </select>至<select style='width: 80px;' id="yearendselect"></select>
            </td>
        </tr>
        <tr id="cycle_month">
            <td style="width: 30px; font-weight: bold; text-align: center;">
                <div id="whichTime">
                    月份:</div>
            </td>
            <td>
                <select id="monthselect">
                </select>至<select id="monthendselect"></select>
            </td>
        </tr>
        <tr>
            <td style="width: 30px; font-weight: bold; vertical-align: top; text-align: center;">
                指标:<br/>
                <input type="checkbox" id="selectAllzb" name="selectAllzb" value="全选"/>
            </td>
            <td>
                <div id="ZbContent">
                </div>
            </td>
        </tr>
        <tr>
            <td style="width: 30px; font-weight: bold; vertical-align: top; text-align: center;">
                维度:<br/>
                <input type="checkbox" id="selectAllwd" name="selectAllwd" value="全选"/>
            </td>
            <td>
                <div id="WdContent">
                </div>
            </td>
        </tr>
    </table>
</div>
<iframe id='frmDownFile' frameborder='0' width='0' height='0' marginheight='0' marginwidth='0'
    scrolling="auto" style="margin: 0px; margin-bottom: -2px;"></iframe>
